package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.GoodsInfoBean;
import com.project.bean.StoreBean;
import com.project.mapper.IGoodsInfoMapper;
import com.project.mapper.IStoreMapper;
import com.project.service.IStoreService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class StoreServiceImpl implements IStoreService {
    @Autowired
    private IStoreMapper storeMapper;
    @Autowired
    private IGoodsInfoMapper goodsInfoMapper;
    @Override
    public IPage<StoreBean> cutByItem(Integer pageNO, String goodsName, String type, String brand, Long supplierId) {
        return storeMapper.cutByItem(new Page<>(pageNO, 3), goodsName, type, brand, supplierId);
    }

    @Override
    public void delete(Long id) {
        storeMapper.deleteById(id);
        QueryWrapper<GoodsInfoBean> qw = new QueryWrapper<>();
        qw.eq("fk_storeId", id);
        goodsInfoMapper.delete(qw);
    }

    @Override
    public List<String> findAllType() {
        return storeMapper.findAllType();
    }

    @Override
    public List<String> findAllBrand() {
        return storeMapper.findAllBrand();
    }

    @Override
    public void updateNum(Long goodsInfoId, Integer num) {
        GoodsInfoBean goodsInfoBean = goodsInfoMapper.selectById(goodsInfoId);
        StoreBean storeBean = storeMapper.selectById(goodsInfoBean.getStoreId());
        Integer i = num - goodsInfoBean.getNum();//改变了多少数量
        goodsInfoBean.setNum(num);
        storeBean.setNum(storeBean.getNum() + i);
        goodsInfoMapper.updateById(goodsInfoBean);
        storeMapper.updateById(storeBean);
    }
}
